//
//  TBRRentDetailController.m
//  TBText
//
//  Created by yy on 2018/4/30.
//  Copyright © 2018年 yy. All rights reserved.
//

#import "TBRRentDetailController.h"
#import <MessageUI/MessageUI.h>
#import "TBRRentController.h"
#import "TBROrderMessageEnumCell.h"
//#import "TBROrderStatusCell.h"
#import "TBRRentDetailCancelOrderCell.h"
#import "TBROrderCarMessageCell.h"
//#import "TBROrderHeadView.h"
#import "TBRGetPlaneMessageModel.h"
#import "TBRHomeModel.h"
#import <YYModel.h>
#import "AppSetting.h"
#import "UILabel+height.h"
#import "UINavigationController+SXFullScreen.h"
@interface TBRRentDetailController ()<UITableViewDelegate, UITableViewDataSource,MFMessageComposeViewControllerDelegate>
@property (nonatomic,strong)NSArray *titleGoOutDesArray;
@property (nonatomic,strong)NSArray *titleGoOutDetailArray;

@property (nonatomic,strong)NSArray *titleCostDesArray;
@property (nonatomic,strong)NSArray *titleCostDetailArray;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation TBRRentDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
    //加载数据
    [self loadData];
}

- (void)setupUI {
    //设置导航条
    [self setupNavItem];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBROrderMessageEnumCell" bundle:nil] forCellReuseIdentifier:@"TBROrderMessageEnumCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBRRentDetailCancelOrderCell" bundle:nil] forCellReuseIdentifier:@"TBRRentDetailCancelOrderCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBROrderCarMessageCell" bundle:nil] forCellReuseIdentifier:@"TBROrderCarMessageCellId"];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 10, 0, 10)];
    }
}

- (void)setupNavItem {
    self.titleText = @"订单详情";
    self.sx_disableInteractivePop = YES;
    [self ISG_NavigationBarWithBackButtonTitle:nil leftImageNamed:nil andAction:@selector(pop2RootC)];
}

- (void)pop2RootC {
    [[NSNotificationCenter defaultCenter] postNotificationName:KRent_detailBackBtnClick_Notice object:nil userInfo:nil];
    
    [[TBRRentAllMessageModel shareAllMessageModel] resetRentMessage];
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[TBRRentController class]]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
}


#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.titleGoOutDetailArray.count;
    }
    if (section == 1 || section == 3) {
        return 1;
    }
    if (section == 2) {
        return self.titleCostDetailArray.count;
    }
    return 1;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //自定义cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self getIdentifier:indexPath] forIndexPath:indexPath];
    [self setupCell:cell indexPath:indexPath];
    return cell;
}

- (NSString *)getIdentifier:(NSIndexPath *)indexPath {
    NSString *iden = @"TBROrderMessageEnumCellId";
    if (indexPath.section == 1) {
        iden = @"TBROrderCarMessageCellId";
    }else if (indexPath.section == 3) {
        //取消订单-------
        iden = @"TBRRentDetailCancelOrderCellId";
    }
    else {
        iden = @"TBROrderMessageEnumCellId";
    }
    return iden;
}

- (void)setupCell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        TBROrderMessageEnumCell *cellEnumM= (TBROrderMessageEnumCell *)cell;
        cellEnumM.lbe_titleDes.text = self.titleGoOutDesArray[indexPath.row];
        cellEnumM.lbe_titleDetail.text = self.titleGoOutDetailArray[indexPath.row];
        if (![UILabel isSingleLineWithContent:self.titleGoOutDetailArray[indexPath.row] andFont:[UIFont systemFontOfSize:15] andWidth:Width - 105]) {
            cellEnumM.lbe_titleDetail.textAlignment = NSTextAlignmentLeft;
        }
    }else if(indexPath.section == 1){
        TBROrderCarMessageCell *cellOrderCarMessage = (TBROrderCarMessageCell *)cell;
        cellOrderCarMessage.orderCarMessageCellBlock = ^(NSInteger tag) {
            if (tag == 10) {
                [self sendMessage];
            }else if (tag == 20) {
                [self callPhone];
            }else {
                
            }
        };
    }
    else if(indexPath.section == 2){
        TBROrderMessageEnumCell *cellEnumM= (TBROrderMessageEnumCell *)cell;
        cellEnumM.lbe_titleDes.text = self.titleCostDesArray[indexPath.row];
        cellEnumM.lbe_titleDetail.text = self.titleCostDetailArray[indexPath.row];
    }else {
        //取消订单-----
        TBRRentDetailCancelOrderCell *cellCancelOrder = (TBRRentDetailCancelOrderCell *)cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {return 158;}
    return indexPath.section == 3 ? 118 : 44;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Width, 10)];
    view.backgroundColor = [UIColor colorWithHex:0xe6e6e6];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}


// MARK: - 加载数据
- (void)loadData {
    [self loadLocalData];
    [self loadNetData];
}
// MARK: - 本地数据
- (void)loadLocalData {
    self.titleGoOutDesArray = @[@"上车时间",@"出发地",@"目的地",@"乘车人",@"备注"];
    if (self.selectedIndex == 4) {
        self.titleGoOutDesArray = @[@"上车时间",@"出发地",@"包车时长",@"乘车人",@"备注"];
    }else if (self.selectedIndex == 5) {
        self.titleGoOutDesArray = @[@"取车时间",@"还车时间",@"取车地点",@"还车地点",@"用车人",@"备注"];
    }
    self.titleCostDesArray = @[@"预估价格",@"支付方式",@"订单号",@"下单时间"];
}

// MARK: - 获取网络数据-----
- (void)loadNetData {
    //获取到网络数据后复制-----------
//    self.estimateDistance = [HelpObject changeNull:@"48.8公里"];
//    self.estimateDuratino = [HelpObject changeNull:@"1小时18分钟"];
    self.estimatePrice = [HelpObject changeNull:@"720.0元"];
    self.payMode = @"储值卡扣取";
    TBRRentAllMessageModel *allMessageModel = [TBRRentAllMessageModel shareAllMessageModel];
    _onCarTime = [HelpObject changeNull:allMessageModel.onCarTime];
    _onPlace = [HelpObject changeNull:allMessageModel.onPlace];
    _offPlace = [HelpObject changeNull:allMessageModel.offPlace];
    _personMessage = [HelpObject changeNull:allMessageModel.showPersonMessageDetail];
    _remark = [HelpObject changeNull:allMessageModel.remark];
    _estimatePrice = [HelpObject changeNull:allMessageModel.estimatePrice];
    _payMode = [HelpObject changeNull:allMessageModel.payMode];
    _orderId = [HelpObject changeNull:allMessageModel.orderId];
    _orderTime = [HelpObject changeNull:allMessageModel.orderTime];
    _charteTimeDuration = [HelpObject changeNull:allMessageModel.charterTimeDuration];
    
    TBRGetPlaneMessageModel *planeMessageModel = allMessageModel.selPlaneMessageModel;
    if (self.selectedIndex == 2) {
        _onCarTime = [NSString stringWithFormat:@"%@航班%@ %@到达后 %@分钟",planeMessageModel.flightNo,planeMessageModel.showselDateStr,planeMessageModel.planArriveTime,planeMessageModel.receptPlaneDuration];
        NSString *tmpStr = planeMessageModel.showPlaneStr;
        _onPlace = [tmpStr componentsSeparatedByString:@"到达"].lastObject;
    }else if (self.selectedIndex == 3) {
        _offPlace = [HelpObject changeNull:allMessageModel.toAirPort];
    }else if (self.selectedIndex == 5) {
        _onCarTime = allMessageModel.startTime;
        _offCarTime = allMessageModel.endTime;
        if (allMessageModel.isGetCarFromShopOnPlace) {
            _onPlace = allMessageModel.isGetCarFromShopOnPlace;
        }
    }
    
    self.titleGoOutDetailArray = @[_onCarTime,_onPlace,_offPlace,_personMessage,_remark];
    if (self.selectedIndex == 4) {
        self.titleGoOutDetailArray = @[_onCarTime,_onPlace,_charteTimeDuration,_personMessage,_remark];

    }else if (self.selectedIndex == 5) {
        self.titleGoOutDetailArray = @[_onCarTime,_offCarTime,_onPlace,_offPlace,_personMessage,_remark];
    }
    self.titleCostDetailArray = @[_estimatePrice,_payMode,_orderId,_orderTime];
    [self.tableView reloadData];
}

- (void)sendMessage {
    if([MFMessageComposeViewController canSendText]) {
        ///短信实例化
        MFMessageComposeViewController * messageVc = [[MFMessageComposeViewController alloc] init];
        ///目标号码，可以多个
        messageVc.recipients = @[@"18888888888",@"15555555555"];
        messageVc.navigationBar.tintColor = [UIColor redColor];
        messageVc.navigationBar.barTintColor = [UIColor redColor];
        messageVc.body = @"发送的内容-发送的内容-发送的内容";
        messageVc.messageComposeDelegate = self;
        [self presentViewController:messageVc animated:YES completion:nil];
        [[[[messageVc viewControllers] lastObject] navigationItem] setTitle:@"title"];
    }
    else {
        NSLog(@"当前设备不支持短信功能");
    }
}

- (void)callPhone{
    ///该方法直接拨打，不会弹出呼叫（取消）
//    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"tel://18888888888"]];
    
    ///该方法会弹出是否取消呼叫
    UIWebView*callWebview =[[UIWebView alloc] init];
    NSURL *telURL =[NSURL URLWithString:@"tel:13373633086"];
    [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
    [self.view addSubview:callWebview];
}

-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    ///dismiss MFMessageComposeViewController
    [controller dismissViewControllerAnimated:YES completion:nil];
    if (result == MessageComposeResultSent) {
        NSLog(@"信息发送成功！");
    }
    else if (result == MessageComposeResultFailed){
        NSLog(@"信息发送失败！");
    }
    else if (result == MessageComposeResultCancelled){
        NSLog(@"点击了取消");
    }
    else{
        
    }
}
@end
